package com.unina.dropplayer.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.UploadRequest;
import com.dropbox.client2.ProgressListener;
import com.dropbox.client2.exception.DropboxException;
import com.unina.dropplayer.common.DropPlayerConstants;
import com.unina.dropplayer.utilities.RootFolderBuilder;

public class UploadTask extends AsyncTask<String, Void, Boolean> {

	private Context mContext;
	private DropboxAPI<?> mApi;
	private Uri Filepath;
	private Toast toast;
	private ProgressListener progressListener;
	private ProgressDialog progressDialog;
	private boolean esito = false;
	private FileInputStream inputStream;
	private UploadRequest uploadRequest;
	String title;

	public String getRealPathFromURI(Context context, Uri contentUri) {
		Cursor cursor = null;
		try {
			String[] proj = { MediaStore.Audio.Media.DATA };
			cursor = context.getContentResolver().query(contentUri, proj, null,
					null, null);
			int column_index = cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
			cursor.moveToFirst();
			return cursor.getString(column_index);
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
	}

	public UploadTask(Context mContext, DropboxAPI<?> mApi, Uri Filepath,
			Toast toast, ProgressListener progressListener,
			ProgressDialog progressDialog) {
		super();
		this.mContext = mContext;
		this.mApi = mApi;
		this.Filepath = Filepath;
		this.toast = toast;
		this.progressListener = progressListener;
		this.progressDialog = progressDialog;
		

	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog.show();
	}

	@Override
	protected Boolean doInBackground(String... params) {
		// TODO Auto-generated method stub

		File file = new File(getRealPathFromURI(mContext, Filepath));
		inputStream = null;
		try {
			inputStream = new FileInputStream(file);
			RootFolderBuilder.createRootFolder(mApi);
			uploadRequest = mApi.putFileRequest("DropPlayer/" + file.getName(), inputStream,
					file.length(), null, progressListener);
			title = file.getName();
			uploadRequest.upload();
			esito = true;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DropboxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return esito;
	}


	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		progressDialog.dismiss();
		if (esito)
			toast = Toast.makeText(mContext, title + " " +
					DropPlayerConstants.MESSAGE_UPLOAD_COMPLETE,
					Toast.LENGTH_SHORT);
		else
			toast = Toast.makeText(mContext, title + " " +
					DropPlayerConstants.MESSAGE_UPLOAD_NOT_COMPLETE,
					Toast.LENGTH_SHORT);
		toast.show();
	}

}
